//
//  RBInvoiceHeaderManageVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/3.
//

#import "RBInvoiceHeaderManageVC.h"
#import "RBInvoiceHeaderListCell.h"
#import "RBInvoiceHeaderAddVC.h"
#import "RBInvoiceHeaderEditVC.h"

@interface RBInvoiceHeaderManageVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UILabel *addBtn;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) RBInvoiceHeaderManageResponse *response;

@end

@implementation RBInvoiceHeaderManageVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"名头管理";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    LCWeakSelf;
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(10), 0, Zoom(10), 0);
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (RBInvoiceHeaderManageModel *model in weakSelf.response.data) {
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.reuseCellIdentifier = NSStringFromClass(RBInvoiceHeaderListCell.class);
                    cellModel.itemSize = CGSizeMake(Zoom(350), Zoom(128));
                    cellModel.cellData = model;
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        if (weakSelf.block) {
                            weakSelf.block(model);
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        }
                    };
                    cellModel.eventBlock1 = ^(NSInteger type, NSInteger index, RBInvoiceHeaderManageModel *data) {
                        [weakSelf cellClickActionWithType:type model:data];
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        group;
    })];
    [self.collectionView reloadData];
    
    if(!self.dataSource.count)
    {
        [self.listView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network

- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            
    } path:_Invoice_list post:YES log:YES responseClass:RBInvoiceHeaderManageResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(RBInvoiceHeaderManageResponse *data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBInvoiceHeaderManageResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

- (void)deleteDataWith:(RBInvoiceHeaderManageModel *)model {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"id"] = model.id;
    } path:_Invoice_delete post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:NO showPlaceHolder:NO sucBlock:^(RBResponse *data) {
        [weakSelf.view showTip:@"删除成功" completion:nil];
        [weakSelf getData];
    } failBlock:^(RBResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event

- (void)addBtnTapped
{
    RBInvoiceHeaderAddVC *vc = RBInvoiceHeaderAddVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)cellClickActionWithType:(NSInteger)type model:(RBInvoiceHeaderManageModel *)model {
    LCWeakSelf;
    switch (type) {
        case 1 : {
            [JKAlert showTitle:@"删除发票" message:@"确定删除此发票吗?" cancelBtn:YES completion:^{
                [weakSelf deleteDataWith:model];
            }];
        }   break;
        case 2 : {
            RBInvoiceHeaderEditVC *vc = [[RBInvoiceHeaderEditVC alloc] init];
            vc.id = model.id;
            [self.navigationController pushViewController:vc animated:YES];
        }   break;
    }
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(self.addBtn.mas_top).offset(-Zoom(10));
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

- (UILabel *)addBtn
{
    if (!_addBtn)
    {
        self.addBtn = ({
            UILabel *textL = [RanTool labelWithFontSize:Zoom(15) weight:1 color:UIColor.whiteColor align:1 text:@"新增名头"];
            textL.backgroundColor = [RBThemeConfig buttonBlueColor];
            [textL rbAddTapGesWithTarget:self action:@selector(addBtnTapped)];
            [textL rbSetCornerRadius:Zoom(22)];
            [self.view addSubview:textL];
            [textL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(44));
                make.width.mas_equalTo(Zoom(340));
                make.bottom.mas_equalTo(-MAX(kRBSafeBottomHeight, Zoom(27)));
            }];
            textL;
        });
    }
    return _addBtn;
}

@end
